package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Bbz
 * @verson: 1.0
 * @Date: 2023/7/7 16:01
 */
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品，同时插入菜品对应的口味数据，需要操作两张表：dish,dish_flavor
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息到套餐表setmeal 因为是继承关系可以保存
        this.save(setmealDto);
        Long setmealId = setmealDto.getId();

        //套餐的菜品
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        dishes=dishes.stream().map((item)->{
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());
        //保存套餐菜品数据到套餐菜品表setmeal_dish
        setmealDishService.saveBatch(dishes);
    }

    /**
     * 查询套餐，将结果封装到SetmealDto
     * @param id
     * @return
     */
    @Override
    @Transactional
    public SetmealDto getByIdWithDishs(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        //对象拷贝,将setmeal的属性拷贝到setmealDto中
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询当前套餐对应的菜品表
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 修改套餐，同时修改套餐对应的菜品数据，需要操作两张表：setmeal,setmeal_dish
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDishs(SetmealDto setmealDto) {
        //关系setmeal表的基本信息
        this.updateById(setmealDto);
        //清理当前套餐对应的菜品数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //添加当前提交套餐对应的菜品数据
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        dishes=dishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(dishes);
    }

    /**
     * 启用或者停用套餐
     * @param status
     * @param id
     */
    @Override
    public void openOrClose(Integer status, Long id) {
        Setmeal setmeal = this.getById(id);
        setmeal.setStatus(status);
        this.updateById(setmeal);
    }

    @Override
    public void deleteWithDishs(Long id) {
        this.removeById(id);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        setmealDishService.remove(queryWrapper);
    }


}
